<template>
  <panel>
    <div class="container">
      <x-row v-if="show">
        <x-col :span="11" class="state-box-parent" :style="{height: chartHeight}">
          <x-row class="state-box">
            <div class="title"><span>保利天悦设备监控中心</span></div>
            <div class="addr"><span>广州市海珠区阅江中路南侧</span></div>
          </x-row>
          <x-row class="state-box1">
            <x-col :span="8">
              <text-box
                titlePlace="bottom"
                size="xlarge"
                title="监控设备数"
                :value="statistics.device">
              </text-box>
            </x-col>
            <x-col :span="8">
              <text-box
                titlePlace="bottom"
                size="xlarge"
                title="监控区域"
                :value="27">
              </text-box>
            </x-col>
            <x-col :span="8">
              <text-box
                titlePlace="bottom"
                size="xlarge"
                title="今日异常"
                type="danger"
                :value="0">
              </text-box>
            </x-col>
          </x-row>
          <x-row class="state-box2">
            <div class="state">
              <span class="lalbel">在线设备:</span>
              <span class="value">{{ statistics.online }}</span>
            </div>
            <div class="state">
              <span class="lalbel">服务故障率:</span>
              <span class="value">0.00%</span>
            </div>
            <div class="state">
              <span class="lalbel">{{ dateNow }}</span>
            </div>
          </x-row>
        </x-col>
        <x-col :span="13" class="failure-statistic">
          <div class="total">{{warningDevices.num}}</div>
          <pie :width="chartWidth" :height="chartHeight" :options="chartOptions"></pie>
        </x-col>
      </x-row>
    </div>
  </panel>
</template>

<script>
import DEVICE_TYPES from 'config/device-types'
import formatDate from 'filters/format-date'

let weekday = new Array(7)
weekday[0] = '星期天'
weekday[1] = '星期一'
weekday[2] = '星期二'
weekday[3] = '星期三'
weekday[4] = '星期四'
weekday[5] = '星期五'
weekday[6] = '星期六'

export default {
  name: 'Statistic',

  data () {
    return {
      show: false,
      chartWidth: '100px',
      chartHeight: '100px',
      dateNow: ''
    }
  },

  computed: {
    chartOptions () {
      return {
        title: {
          text: '服务异常事件统计',
          x: 'center',
          bottom: 10,
          textStyle: {
            color: '#999',
            fontSize: 12,
            fontWeight: 'normal'
          }
        },
        tooltip: {
          trigger: 'item',
          formatter: '{a} <br/>{b} : {c} ({d}%)',
          position: ['30%', '52%']
        },
        series: [{
          radius: ['40%', '60%'],
          center: ['50%', '45%'],
          data: this.warningDevices.detail
        }]
      }
    },
    waterDevices () {
      return this.$store.getters.tubes
    },
    electricalDevices () {
      return this.$store.getters.phase
    },
    phoneDevices () {
      return this.$store.getters.phonesDevices
    },
    normalDevicesNum () {
      // let all = [].concat(this.waterDevices, this.electricalDevices)
      let water = _.filter(this.waterDevices, (item) => {
        return !item.isWarnning && item.type === DEVICE_TYPES.TUBE_PRESSURE && item.online
      })
      let electrical = _.filter(this.electricalDevices, (item) => {
        return !item.isWarnning && item.type === DEVICE_TYPES.PHASE && item.online
      })
      let phone = _.filter(this.phoneDevices, (item) => {
        return item.status - 0 === 1
      })
      return water.length + electrical.length + phone.length
    },
    warningDevices () {
      // let water = _.filter(this.waterDevices, (item) => {
      //   return item.isWarnning && item.type === DEVICE_TYPES.TUBE_PRESSURE && item.online
      // })
      // let electrical = _.filter(this.electricalDevices, (item) => {
      //   return item.isWarnning && item.type === DEVICE_TYPES.PHASE && item.online
      // })
      return {
        num: 0,
        detail: [{
          name: '给排水故障',
          value: 0
        }, {
          name: '供配电故障',
          value: 0
        }]
      }
    },
    offlineDevicesNum () {
      let onlineWater = _.filter(this.waterDevices, (item) => {
        return item.type === DEVICE_TYPES.TUBE_PRESSURE && item.online
      })
      let onlineElectrical = _.filter(this.electricalDevices, (item) => {
        return item.type === DEVICE_TYPES.PHASE && item.online
      })
      let onlinePhone = _.filter(this.phoneDevices, (item) => {
        return item.status - 0 === 1
      })
      return 42 + this.phoneDevices.length - onlineWater.length - onlineElectrical.length - onlinePhone.length
    },
    statistics () {
      let deviceNum = this.normalDevicesNum + this.warningDevices.num + this.offlineDevicesNum
      return {
        device: deviceNum,
        online: this.normalDevicesNum + this.warningDevices.num,
        warningRate: Number(this.warningDevices.num * 100 / deviceNum).toFixed(2)
      }
    }
  },

  mounted () {
    this.getCurDate()
    setTimeout(this.initialize, 500)
    this.initLocalStorageDatas()
  },

  methods: {
    getCurDate () {
      let date = new Date()
      let time = formatDate(date, 'yyyy-MM-dd', true)
      let week = weekday[date.getDay()]
      this.dateNow = time + ' ' + week
    },
    initLocalStorageDatas () {
      // let allDevices = JSON.parse(window.localStorage.getItem('electricalDevices'))
      // if (Array.isArray(allDevices)) {
      //   this.electricalDevices = allDevices
      // }
      // allDevices = JSON.parse(window.localStorage.getItem('waterDevices'))
      // if (Array.isArray(allDevices)) {
      //   this.waterDevices = allDevices
      // }
      // allDevices = JSON.parse(window.localStorage.getItem('phoneDevices'))
      // if (Array.isArray(allDevices)) {
      //   this.phoneDevices = allDevices
      // }
    },
    initialize () {
      this.chartWidth = `${(this.$el.clientWidth - 35) / 24 * 13}px`
      this.chartHeight = `${this.$el.clientHeight}px`

      this.show = true
    }
  }
}
</script>

<style lang="stylus" scoped>
@import '../../../../assets/stylus/common'

.container {
  padding-left: 35px;
}

.state-box-parent
  position relative
  min-height 200px
.state-box
  height: 40%
  display: flex;
  flex-direction: column;
  justify-content: center;
  .title
    font-size 24px
    font-weight 400
  .addr
    font-size 12px
    color #6B6B6B
.state-box1
  height: 33%
  display: flex;
  flex-direction: row;
  justify-content: center;
.state-box2
  position absolute
  bottom 0
  left 0px
  height 40px
  width 100%
  font-size 10px
  color #6B6B6B
  border-top 1px dashed
  padding-top 10px
  .state
    display inline-block
    margin-right 10px
    .value
      color #fff

.failure-statistic {
  position: relative;

  .total {
    absolute: left top 45%;
    margin-top: -20px;
    size: 100% 40px;
    font-size: 30px;
    line-height: 40px;
    text-align: center;
  }
}
</style>
